package cn.chinasyq.photoquan.user.mode;

import com.ta.internal.multipart.upload.HttpType;
import com.ta.internal.multipart.upload.UploadParams;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.master.volley.models.pojo.Wrapper;
import cn.master.volley.models.response.handler.ResponseHandler;
import cn.master.volley.models.response.listener.ResolveJson;
import cn.master.volley.models.response.listener.ResponseForMultiPartListener;

/**
 * Created by 一搏 on 2015/11/27.
 */
public class UploadFileModel {
    public static UploadParams addParam(String key, String value)
    {
        UploadParams param = new UploadParams();
        param.setParamKey(key);
        param.setParamValue(value);
        return param;
    }

    public static <T> ResponseForMultiPartListener<Wrapper<T>> uploadFilesAndParams(ResolveJson<T> re, HttpType httpType, String url, HashMap<String, File> files, HashMap<String, String> param, ResponseHandler handler) {
        List<UploadParams> fileparams= new ArrayList<>();

        List<UploadParams> moreparams= new ArrayList<>();

        try {
            for (Map.Entry<String, File> entry : files.entrySet()) {
                fileparams.add(addParam(entry.getKey(),entry.getValue().getAbsolutePath()));
            }
            for (Map.Entry<String, String> entry : param.entrySet()) {
                moreparams.add(addParam(entry.getKey(),entry.getValue()));
            }
        } catch ( Exception e ) {
            e.printStackTrace();
        }

        ResponseForMultiPartListener<Wrapper<T>> tamultipartuploader = (ResponseForMultiPartListener<Wrapper<T>>) new ResponseForMultiPartListener<>(handler,re,httpType, url, fileparams, moreparams);
        tamultipartuploader.exequte();
        return tamultipartuploader;
    }
}
